package com.aiju.cash.ui.setting;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.common.ActivityStack;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.SimpleDialog;
import com.aiju.widgit.layout.SettingItemTextView;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-19 17:52
 * @version: V1.0
 * @Description: 店员详情
 */
public class ClerkInfoActivity extends BaseTitleActivity {

    private ShopClerk mClerk;

    private SettingItemTextView idTextView, nameTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clerk_info);
        setCenterTitle("店员详情");
        mClerk = (ShopClerk) getIntent().getSerializableExtra("clerk");
        idTextView = (SettingItemTextView) findViewById(R.id.tv_clerk_id);
        nameTextView = (SettingItemTextView) findViewById(R.id.tv_clerk_name);

        //店员显示删除
        if (mClerk.isClerk()) {
            findViewById(R.id.btn_clerk_delete).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_clerk_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog();
                }
            });

            Drawable nav_up = getResources().getDrawable(R.mipmap.icon_setting_right);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            nameTextView.getRightView().setCompoundDrawables(null, null, nav_up, null);
            nameTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ClerkInfoActivity.this, StoreNameActivity.class);
                    intent.putExtra("clerk", mClerk);
                    intent.putExtra("showName", mClerk.getUserName());
                    intent.putExtra("showType", StoreNameActivity.SHOW_CLERK);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        idTextView.setRightText(mClerk.getPhone());
        nameTextView.setRightText(mClerk.getUserName());
    }

    private void showDialog(){
        new SimpleDialog(this)
                .setLayoutParams()
                .setTitileText("确定删除该店员？")
                .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {

                        userDelete();
                    /*    findViewById(R.id.btn_good_add_save).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mDialogHelper.showLoadingProgressDialog();

                                L.d(mClerk.getOperatorId());
                            }
                        });*/
                    }
                }).show();
    }

    private void userDelete(){
        mDialogHelper.showLoadingProgressDialog();
        new LoginApi().userDelete(mClerk.getOperatorId(), mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    T.ToastShort("成功删除收银员");
                    ActivityStack.cleanTop(2);
                    startActivity(ClerkManagerActivity.class);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }


}
